package com.fm.common.wediget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.recyclerview.widget.RecyclerView


/**
 *
 * @Author:         ly2
 * @CreateDate:     2021/1/18
 * @Description:    可更改颜色分割线
 *
 */
class SimpleDividerDecoration(
    context: Context,
    @ColorRes private val color: Int,
    private val space: Int
) : RecyclerView.ItemDecoration() {
    private val dividerPaint: Paint = Paint()

    init {
        dividerPaint.color = ContextCompat.getColor(context, color)
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.bottom = space
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight
        for (child in parent.children) {
            val top = child.bottom.toFloat()
            val bottom = (child.bottom + space).toFloat()
            c.drawRect(left.toFloat(), top, right.toFloat(), bottom, dividerPaint)
        }
    }
}